import http from '@/util/http'
export default {
  state: {
    display: false,
    currentSession: 'User:1',
    me: {
      group: 'User',
      id: 3,
      name: '我是玩家',
      img: '',
      intro: '简介简介的时间发生的空间发动机离开的撒娇啊可是老大'
    },
    sessions: [{
      badge: 0,
      user: {
        group: 'Shop',
        id: 20,
        name: '一家金币',
        img: '',
        intro: '简介简介的时间发生的空间发动机离开的撒娇啊可是老大'
      },
      messages: [
        {
          text: '你好1',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: '你好2',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: '你好3',
          self: false,
          time: '2016-10-20 12:14'
        }, {
          text: '你好5',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: '你好5',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: '你好5',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: '你好5',
          self: false,
          time: '2016-10-20 12:14'
        }
      ]
    }, {
      badge: 0,
      user: {
        group: 'Shop',
        id: 10,
        name: '诚信金币',
        img: '',
        intro: 'sdafadsfjklkfkdssdflklksdklsdlfdlk;s'
      },
      messages: [
        {
          text: 'dsasd',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: 'adsdfs',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: 'fdsafsdafdas',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: 'afsdfasddfsa',
          self: false,
          time: '2016-10-20 12:14'
        }, {
          text: 'afsfsda',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: '你好5',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: 'sadfsdf',
          self: false,
          time: '2016-10-20 12:14'
        }
      ]
    }, {
      badge: 0,
      user: {
        group: 'Shop',
        id: 8,
        name: '诚信2金币',
        img: '',
        intro: 'sdafadsfjklkfkdssdflklksdklsdlfdlk;s'
      },
      messages: [
        {
          text: 'dsasd',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: 'adsdfs',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: 'fdsafsdafdas',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: 'afsdfasddfsa',
          self: false,
          time: '2016-10-20 12:14'
        }, {
          text: 'afsfsda',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: '你好5',
          self: true,
          time: '2016-10-20 12:14'
        }, {
          text: 'sadfsdf',
          self: false,
          time: '2016-10-20 12:14'
        }
      ]
    }, {
      badge: 3,
      user: {
        group: 'User',
        id: 1,
        name: '我自己',
        img: '',
        intro: 'sdafadsfjklkfkdssdflklksdklsdlfdlk;s'
      },
      messages: []
    }
    ]
  },
  getters: {
    'GET_MESSAGES': function (state) {
      let session = state.sessions.find(function (value, index, arr) {
        return value.user.group + ':' + value.user.id === state.currentSession
      })
      return session.messages
    },
    'GET_USER_INFO': function (state) {
      let session = state.sessions.find(function (value, index, arr) {
        return value.user.group + ':' + value.user.id === state.currentSession
      })
      return session.user
    },
    'GET_SESSIONS': function ({sessions}) {
      return sessions
    },
    'GET_WINDOW_STATUS': function ({display}) {
      return display
    }
  },
  mutations: {
    'ADD_MESSAGE' (state, {time, text, self}) {
      let session = state.sessions.find(function (value, index, arr) {
        return value.user.group + ':' + value.user.id === state.currentSession
      })
      session.messages.push({
        time: time,
        text: text,
        self: self
      })
      session.badge += 1
    },
    'GET_MESSAGE' (state, session) {

    },
    'SHOW_WINDOW' (state, session) {
      state.display = true
      state.currentSession = session
    },
    'CLOSE_WINDOW' (state, vm) {
      state.display = false
    }
  },
  actions: {
    fecthMessages (context) {
      http.get('api').then(function (res) {
        console.log(res)
      })
    }
  }
}
